package com.hnkywl.sts.dao.cwgl.kmys;

import com.hnkywl.sts.entity.cwgl.Kmdm;
import com.hnkywl.sts.entity.cwgl.kmys.Tyfzhsmc;
import com.hnkywl.sts.entity.organ.Wls;
import com.hnkywl.sts.entity.sys.Js;
import com.hnkywl.sts.entity.zjgl.Pj;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.hibernate.tool.hbm2x.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 说明：
 * 包:        com.hnkywl.sts.dao.sys
 * 类描述:    角色dao类
 * 修改备注: [说明本次修改内容]
 * 版本:        [v1.0]
 */
@Repository
public class TyfzhsmcDao extends SimpleHibernateTemplate<Tyfzhsmc, Long> {
    /**
     * 查询角色列表
     *
     * @param
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<Tyfzhsmc> findJsList(String mc, int pageNo, int pageSize) {
//        Page<Tyfzhsmc> page = new Page<Tyfzhsmc>();
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Tyfzhsmc> page = new Page<Tyfzhsmc>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (mc != null && !("").equals(mc)) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" mc like ?");
            param.add("%"+mc.trim()+"%");
        }
        //==============记录数量===============
        hql = "select count(*) from Tyfzhsmc where 1=1" ;
        if (condition.length() != 0)
            hql = hql + " and " + condition ;
        hql = hql +" order by lastUpdated desc";
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Tyfzhsmc where 1=1"  ;
        if (condition.length() != 0)
            hql = hql + " and " + condition ;
        hql = hql +" order by lastUpdated desc";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }

    public List<Tyfzhsmc> findAllRootMc() {
        return find("from Tyfzhsmc ");
    }

    /**
     * 对角色名称进行校验
     *
     * @param propertyName 角色名称
     * @param id           角色ID
     * @return
     */
    public Boolean checkUnique(Long id, Object propertyValue, String propertyName) {
        String hql;
        if (propertyValue == null)
            return false;
        if (id == null) {
            //修改检验
            hql = "from Tyfzhsmc where " + propertyName + " = ? and deleted!=1";
            return this.existsQuery(hql, propertyValue);
        } else {
            //新增检验
            hql = "from Tyfzhsmc where " + propertyName + " = ? and id <> ? and deleted!=1";
            return this.existsQuery(hql, propertyValue, id);
        }
    }

    public String findAllTyfzhsmcTree(String dl) {
        try {
            String nodes = "[";
            StringBuffer json = new StringBuffer();
            String hql = "from Tyfzhsmc where dl = ?  ";
            List<Tyfzhsmc> result = find(hql, dl);
            if (result != null && !result.isEmpty()) {
                for (int i = 0; i < result.size(); i++) {
                    Tyfzhsmc k = result.get(i);
                    json.append("{\"id\":\"");
                    json.append(k.getId());
                    json.append("\",\"text\":\"");
                    json.append(k.getMc());
                    json.append("\",\"attributes\":{\"code\":\"" + k.getCode() + "\"}");
                    json.append("},");
                }
                nodes += json.substring(0, json.length() - 1);
            }
            nodes += "]";
            return nodes;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "";
    }
}
